﻿using Microsoft.Phone.Controls;
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;

namespace TheVerge.Phone.Controls
{
    public partial class HeaderControl : UserControl
    {
        private static List<WeakReference> _headers = new List<WeakReference>();

        public HeaderControl()
        {
            InitializeComponent();
            _headers.Add(new WeakReference( this));
            UpdateVisibility();
        }

        public static void NotifyThemeUpdated()
        {
            var headers = _headers.ToArray();
            HeaderControl header = null;
            foreach (var headerRef in headers)
            {
                if(headerRef.IsAlive && (null != (header = headerRef.Target as HeaderControl)))
                    header.UpdateVisibility();
            }
        }

        private void UpdateVisibility()
        {
            light.Visibility = Theme.Active.Background == ThemeBackground.Light ? Visibility.Visible : Visibility.Collapsed;
            dark.Visibility = Theme.Active.Background == ThemeBackground.Dark ? Visibility.Visible : Visibility.Collapsed;
        }
    }
}
